4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
11 // You must not remove this notice, or any other, from this software.
16 namespace Microsoft
.JScript
{
18 using Microsoft
.JScript
.Vsa
;
20 using System
.Collections
;
21 using System
.Reflection
;
22 using System
.Security
;
23 using System
.Security
.Permissions
;
26 internal class VsaScriptScope
: VsaItem
, IVsaScriptScope
29 private VsaScriptScope parent
;
30 private GlobalScope scope
;
31 private ArrayList items
;
32 private bool isCompiled
;
33 private bool isClosed
;
35 internal VsaScriptScope(VsaEngine engine
, string itemName
, VsaScriptScope parent
)
36 : base(engine
, itemName
, (VsaItemType
)(int)VSAITEMTYPE2
.SCRIPTSCOPE
, VsaItemFlag
.None
){
39 this.items
= new ArrayList(8);
40 this.isCompiled
= false;
41 this.isClosed
= false;
44 public virtual Object
GetObject(){
45 if (null == this.scope
)
46 if (null != this.parent
)
47 this.scope
= new GlobalScope((GlobalScope
)this.parent
.GetObject(), this.engine
, false);
49 this.scope
= new GlobalScope(null, this.engine
);
53 public IVsaScriptScope Parent
{
59 public virtual IVsaItem
AddItem(string itemName
, VsaItemType type
){
63 throw new VsaException(VsaError
.EngineClosed
);
64 if (null != GetItem(itemName
))
65 throw new VsaException(VsaError
.ItemNameInUse
);
69 case (int)VSAITEMTYPE2
.HOSTOBJECT
:
70 case (int)VSAITEMTYPE2
.HOSTSCOPE
:
71 case (int)VSAITEMTYPE2
.HOSTSCOPEANDOBJECT
:
72 item
= new VsaHostObject(this.engine
, itemName
, (VsaItemType
)type
, this);
73 if (type
== (VsaItemType
)VSAITEMTYPE2
.HOSTSCOPE
||
74 type
== (VsaItemType
)VSAITEMTYPE2
.HOSTSCOPEANDOBJECT
){
75 ((VsaHostObject
)item
).exposeMembers
= true;
77 if (type
== (VsaItemType
)VSAITEMTYPE2
.HOSTOBJECT
||
78 type
== (VsaItemType
)VSAITEMTYPE2
.HOSTSCOPEANDOBJECT
)
79 ((VsaHostObject
)item
).isVisible
= true;
80 if (this.engine
.IsRunning
){
81 ((VsaHostObject
)item
).Compile();
82 ((VsaHostObject
)item
).Run();
86 case (int)VSAITEMTYPE2
.SCRIPTSCOPE
:
87 item
= new VsaScriptScope(this.engine
, itemName
, this);
92 //if (!this.engine.IsRunning)
95 throw new VsaException(VsaError
.ItemTypeNotSupported
);
99 public virtual IVsaItem
GetItem(string itemName
){
100 for (int i
= 0, n
= this.items
.Count
; i
< n
; i
++){
101 VsaItem item
= (VsaItem
)this.items
[i
];
102 if (null == item
.Name
&& null == itemName
|| null != item
.Name
&& item
.Name
.Equals(itemName
))
103 return (IVsaItem
)this.items
[i
];
108 public virtual void RemoveItem(string itemName
){
109 for (int i
= 0, n
= this.items
.Count
; i
< n
; i
++){
110 VsaItem item
= (VsaItem
)this.items
[i
];
111 if (null == item
.Name
&& null == itemName
|| null != item
.Name
&& item
.Name
.Equals(itemName
)){
113 this.items
.Remove(i
);
117 throw new VsaException(VsaError
.ItemNotFound
);
120 public virtual void RemoveItem(IVsaItem item
){
121 for (int i
= 0, n
= this.items
.Count
; i
< n
; i
++){
122 VsaItem vsaItem
= (VsaItem
)this.items
[i
];
123 if (vsaItem
== item
){
125 this.items
.Remove(i
);
129 throw new VsaException(VsaError
.ItemNotFound
);
132 public virtual int GetItemCount(){
133 return this.items
.Count
;
136 public virtual IVsaItem
GetItemAtIndex(int index
){
137 if (index
< this.items
.Count
)
138 return (IVsaItem
)this.items
[index
];
140 throw new VsaException(VsaError
.ItemNotFound
);
143 public virtual void RemoveItemAtIndex(int index
){
144 if (index
< this.items
.Count
){
145 ((VsaItem
)this.items
[index
]).Remove();
146 this.items
.Remove(index
);
148 throw new VsaException(VsaError
.ItemNotFound
);
151 public virtual IVsaItem
CreateDynamicItem(string itemName
, VsaItemType type
){
152 if (this.engine
.IsRunning
)
153 return AddItem(itemName
, type
);
155 throw new VsaException(VsaError
.EngineNotRunning
);
158 internal override void CheckForErrors(){
159 if (this.items
.Count
== 0)
162 this.engine
.Globals
.ScopeStack
.Push((ScriptObject
)GetObject());
163 // compile all the items in this scope
164 foreach (Object item
in this.items
)
165 ((VsaItem
)item
).CheckForErrors();
167 this.engine
.Globals
.ScopeStack
.Pop();
171 internal override void Compile(){
172 if (this.items
.Count
== 0)
174 if (!this.isCompiled
){
175 this.isCompiled
= true;
177 this.engine
.Globals
.ScopeStack
.Push((ScriptObject
)GetObject());
179 // compile all the items in this scope
180 foreach (Object item
in this.items
)
181 ((VsaItem
)item
).Compile();
183 this.engine
.Globals
.ScopeStack
.Pop();
186 this.isCompiled
= false;
192 internal override void Reset(){
194 foreach (Object item
in this.items
)
195 ((VsaItem
)item
).Reset();
198 internal void ReRun(GlobalScope scope
){
199 foreach (Object item
in this.items
)
200 if (item
is VsaHostObject
)
201 ((VsaHostObject
)item
).ReRun(scope
);
202 if (this.parent
!= null)
203 this.parent
.ReRun(scope
);
206 internal override void Run(){
207 if (this.items
.Count
== 0)
210 this.engine
.Globals
.ScopeStack
.Push((ScriptObject
)GetObject());
211 foreach (Object item
in this.items
)
212 ((VsaItem
)item
).Run();
214 this.engine
.Globals
.ScopeStack
.Pop();
218 internal override void Close(){
219 foreach (Object item
in this.items
)
220 ((VsaItem
)item
).Close();
224 this.isClosed
= true;